﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Business.Campaigns;
using iOak.Business.Resumes;
using iOak.Domain.Campaigns;
using iOak.Common.Untity;

namespace iOak.Web
{
    public partial class ResumeEdit : BasePage
    {
        private static readonly ResumeService itemResumeService = new ResumeService();
        private static readonly ApplicationService itemApplicationService = new ApplicationService();
        private static readonly CampaignService itemCampaignService = new CampaignService();
        private static readonly ResumeTemplateService itemResumeTemplateService = new ResumeTemplateService();
        private static readonly PolicyService itemPolicyService = new PolicyService();
        private static readonly ApplicationCandidateService itemApplicationCandidateService = new ApplicationCandidateService();
        private WorkAssistant _workassistant = new WorkAssistant();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hidResumeId.Value = Request.QueryString["ResumeId"];
                hidApplicationCandidatesId.Value = Request.QueryString["ApplicationCandidatesId"];
                hidCampaignId.Value = Request.QueryString["campaignId"];
                Guid resumeTemplateId = Guid.Empty;
                Guid applicationId = Guid.Empty;
                if (string.IsNullOrWhiteSpace(hidResumeId.Value))
                {
                    var itemCampaign = itemCampaignService.Retrieve(new Guid(hidCampaignId.Value));
                    CheckIsOverdue(itemCampaign);
                    lblTitle.Text = "添加候选人";
                    var itemApplication = itemApplicationService.GetListByCampaignIdAndCompanyId(new Guid(hidCampaignId.Value),
                        _workassistant.CurrentUser.CompanyId);
                    if (itemApplication == null)
                    {
                        // 创建
                        itemApplication = new ApplicationBase();
                        itemApplication.ApplicationId = Guid.NewGuid();
                        itemApplication.AppliedBy = _workassistant.CurrentUser.UserId;
                        itemApplication.AppliedDate = DateTime.Now;
                        itemApplication.CampaignId = new Guid(hidCampaignId.Value);
                        itemApplication.CompanyId = _workassistant.CurrentUser.CompanyId;
                        itemApplication.Status = (int)ApplicationStatus.Open;

                        itemApplicationService.Create(itemApplication);
                    }
                    applicationId = itemApplication.ApplicationId;
                    
                    var itemPolicy = itemPolicyService.Retrieve(itemCampaign.PolicyId);
                    resumeTemplateId = itemPolicy.ResumeTemplateId;
                    hidResumeId.Value = Guid.Empty.ToString();
                }
                else
                {
                    var itemApplicationCandidate = itemApplicationCandidateService.Retrieve(new Guid(hidApplicationCandidatesId.Value));
                    var itemApplication = itemApplicationService.Retrieve(itemApplicationCandidate.ApplicationId);
                    var itemCampaign = itemCampaignService.Retrieve(itemApplication.CampaignId);
                    CheckIsOverdue(itemCampaign);
                    lblTitle.Text = "修改候选人信息";
                    // 通过CampaignId获取模板ID
                    var itemResume = itemResumeService.Retrieve(new Guid(hidResumeId.Value));
                    resumeTemplateId = itemResume.ResumeTemplateId;
                    applicationId = itemApplicationCandidate.ApplicationId;
                    lblNote.Text = itemApplicationCandidate.Notes;
                    tableNote.Visible = !String.IsNullOrWhiteSpace(lblNote.Text);
                }
                hidresumeTemplateId.Value = resumeTemplateId.ToString();
                var templateItem = itemResumeTemplateService.Retrieve(resumeTemplateId);
                hidPath.Value = templateItem.TemplatePath;
                hidapplicationId.Value = applicationId.ToString();
            }

            // 加载用户控件
            BaseUserControl detialControl = LoadControl(hidPath.Value) as BaseUserControl;
            divDetial.Controls.Add(detialControl);
            detialControl.BindData(new Guid(hidresumeTemplateId.Value), hidResumeId.Value, new Guid(hidapplicationId.Value), hidApplicationCandidatesId.Value);
        }

        //判断是否超期
        public void CheckIsOverdue(Campaign campaign) 
        {
            bool overDue = false;
            //如果当前时间已超过活动截止日期，则不允许提交
            if (campaign.EndDate.CompareTo(DateTime.Now.Date) < 0)
            {
                overDue = true;
            }
            //如果当前时间已超过活动截止日期，则重定向到一览画面
            if (overDue)
            {
                Response.Redirect("/TalentDeclareList.aspx");
            }
        }
    }
}